package it.univaq.mwt.tp;

import it.univaq.mwt.tp.Customer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.persistence.*;

import static javax.persistence.FetchType.EAGER;
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.CascadeType.REFRESH;
import static javax.persistence.TemporalType.TIMESTAMP;

/**
 * Entity implementation class for Entity: Order
 * 
 */
@Entity
@Table(name = "ORDER0")
public class Order implements Serializable {

	private int orderID;
	private Date orderDate;
	private Customer customer;
	private Collection<OrderLine> orderLines = new ArrayList<OrderLine>();
	private static final long serialVersionUID = 1L;

	public Order() {
		super();
	}

	public Order(int orderID) {
		this.orderID = orderID;
		this.orderDate = new Date();
	}

	@Id
	public int getOrderID() {
		return this.orderID;
	}

	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}

	@Temporal(TIMESTAMP)
	public Date getOrderDate() {
		return this.orderDate;
	}

	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}

	@OneToOne(cascade = { REFRESH, PERSIST }, optional = false)
	public Customer getCustomer() {
		return this.customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	@OneToMany(fetch = EAGER, cascade = PERSIST)
	public Collection<OrderLine> getOrderLines() {
		return this.orderLines;
	}

	public void setOrderLines(Collection<OrderLine> orderLines) {
		this.orderLines = orderLines;
	}

	public void addOrderLine(OrderLine orderLine) {
		orderLines.add(orderLine);
	}
	
	@Transient
	public float getTotal() {
		float total = (float)0.0;
		Iterator<OrderLine> iter = orderLines.iterator();
		while (iter.hasNext())
			total += iter.next().getLineTotal();
		return total;
	}

}
